<?php
class Entry
{
	private $entryid=0;
	private $entryTitle;
	private $entryNote;
	private $calorieCount;
	private $carbCount;
	private $date;
	private $userId;
	private $dbConn;
	
	public function __construct
	(
			$id, 
			$title, 
			$note, 
			$calCount, 
			$carbCount, 
			$dt, 
			$uid, 
			$connection
	)
	{
		$this->entryid = $id;
		$this->entryTitle = urldecode ($title);
		
		if(is_null($note) || empty($note))
			$this->entryNote = "";
		else
			$this->entryNote = urldecode ($note);
		
		$this->calorieCount = $calCount;
		
		if(is_null($carbCount) || empty($carbCount))
			$this->carbCount = 0;
		else
			$this->carbCount = $carbCount;
		
		$this->date = $dt;
		$this->userId = $uid;
		$this->dbConn = $connection;
	}
	
	public function save()
	{
		if(! is_numeric($this->entryid) || $this->entryid < 1)
		{
			$this->create();
		}	else
		{
			$this->update();
		}
		
	}
	
	public function create()
	{
		
		
		$sql =
		"insert into food_entry
		(entry_date, active_flag, food_entry_user_id, food_item, notes, calories, carbs)
		values
		 (:mysqlDate, 'Y', :userId, :foodName, :foodDescr, :foodCalories, :foodCarbs)";
		
		$ps = $this->dbConn->prepare($sql);
		$ps->bindParam(':mysqlDate', $this->date);
		$ps->bindParam(':userId', $this->userId);
		$ps->bindParam(':foodName', $this->entryTitle);
		$ps->bindParam(':foodDescr', $this->entryNote);
		$ps->bindParam(':foodCalories', $this->calorieCount);
		$ps->bindParam(':foodCarbs', $this->carbCount);
		$ps->execute();
		$this->entryid = $this->dbConn->lastInsertId();
		//echo $this->entryid;
	}
	
	public function update()
	{
		$sql =
		"update food_entry 
			set food_item=:foodName, 
			    notes=:foodDescr, 
			    calories=:foodCalories, 
			    entry_date=:mysqlDate,
			    carbs=:foodCarbs
			where food_entry_id = :entryid and food_entry_user_id=:uid";
		$ps = $this->dbConn->prepare($sql);
		$ps->bindParam(':foodName', $this->entryTitle);
		$ps->bindParam(':foodDescr', $this->entryNote);
		$ps->bindParam(':foodCalories', $this->calorieCount);
		$ps->bindParam(':mysqlDate', $this->date);
		$ps->bindParam(':foodCarbs', $this->carbCount);
		$ps->bindParam(':entryid', $this->entryid);
		$ps->bindParam(':uid', $this->userId);
		$ps->execute();
		
	}
	
	public function toValueObject()
	{
		$result = new EntryVO();
		$result->entryid = $this->entryid;
		$result->date= $this->date;
		$result->title = $this->entryTitle;
		$result->calories = $this->calorieCount;
		$result->carbs = $this->carbCount;
		$result->notes = $this->entryNote;
		return $result;
		
	}
	
	public static function delete($entryid, $userId, $conn)
	{
		$ps = $conn->prepare("delete from food_entry where food_entry_id=:entryid and food_entry_user_id=:uid");
		$ps->bindParam(':entryid', $entryid);
		$ps->bindParam(':uid', $userId);
		$ps->execute();
		return "success";
	}
	
	public static function fetchEntries($uid, $mySQLDate, $conn)
	{
		$dateFormat ='m/d/Y';
		$result = array();
		$d = new DateTime($mySQLDate);
		$formattedDate = $d->format($dateFormat);
		$sql = " select food_entry_id, food_item ,notes, calories, carbs  
				from food_entry 
				where food_entry_user_id = :userId 
				and entry_date = :dateField ";
		$ps = $conn->prepare($sql);
		$ps->bindParam(':userId', $uid);
		$ps->bindParam(':dateField', $mySQLDate);
		$ps->execute();
		
		while ($row = $ps->fetch(PDO::FETCH_ASSOC))
		{
			$o = new EntryVO();
			$o->entryid = $row["food_entry_id"];
			$o->title = $row["food_item"];
			$o->notes = $row["notes"];
			$o->calories = $row["calories"];
			$o->carbs = $row["carbs"];
			$o->date = $formattedDate;
			array_push($result, $o);
			
		}
		return $result;
	}
	
	public static function fetchEntry($uid, $entryid, $conn)
	{
		$result = new EntryVO();
		$dateFormat ='F d Y';
		$sql = " select food_item ,notes, calories, carbs, entry_date
				from food_entry
				where food_entry_user_id = :userId
				and food_entry_id = :foodentryid ";
		$ps = $conn->prepare($sql);
		$ps->bindParam(':userId', $uid);
		$ps->bindParam(':foodentryid', $entryid);
		$ps->execute();
	
		while ($row = $ps->fetch(PDO::FETCH_ASSOC))
		{
			
			$result->entryid = $entryid;
			$result->title = $row["food_item"];
			$result->notes = $row["notes"];
			$result->calories = $row["calories"];
			$result->carbs = $row["carbs"];
			$d = new DateTime($row["entry_date"]);
			$formattedDate = $d->format($dateFormat);
			$result->date = $formattedDate;
		}
		return $result;
	}
	
	
}